package com.itheima.service.product.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.Product;
import com.itheima.factory.MapperFactory;
import com.itheima.mapper.product.ProductMapper;
import com.itheima.service.product.ProductService;
import com.itheima.utils.TransactionUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;


public class ProductServiceImpl implements ProductService {

    @Override
    public Page<Product> findAll(String pageNum, String pageSize) {

        SqlSession session = null;
        Page<Product> page = null;
        try {
            //调用工具类获取session对象
            session = MapperFactory.getSqlSession();
            //获取动态代理对象
            ProductMapper mapper = MapperFactory.getMapper(session, ProductMapper.class);
            //默认当前页为1
            int pageN = 1;
            //默认每页显示条目数为10
            int pageS = 10;
            //对获取的当前页码和每页显示条目数进来类型处理
            if (StringUtils.isNotBlank(pageNum)) {
                pageN = Integer.parseInt(pageNum);
            }
            if (StringUtils.isNotBlank(pageSize)) {
                pageS = Integer.parseInt(pageSize);
            }
            //设置分页参数：当前页码，每页显示的条目数
            page = PageHelper.startPage(pageN, pageS);
            //调用mapper方法
            mapper.findAll();

        } catch (Exception e) {
            //回滚
            TransactionUtil.rollback(session);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(session);
        }
        return page;
    }

    @Override
    public void save(Product product) {
        SqlSession session = null;
        try {
            //调用工具类获取session对象
            session = MapperFactory.getSqlSession();
            //获取动态代理对象
            ProductMapper mapper = MapperFactory.getMapper(session, ProductMapper.class);
            //调用mapper方法
            mapper.save(product);
            //提交
            TransactionUtil.commit(session);
        } catch (Exception e) {
            //回滚
            TransactionUtil.rollback(session);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(session);
        }
    }

    @Override
    public Product findById(String id) {
        SqlSession session = null;
        Product product = null;
        try {
            //调用工具类获取session对象
            session = MapperFactory.getSqlSession();
            //获取动态代理对象
            ProductMapper mapper = MapperFactory.getMapper(session, ProductMapper.class);
            //调用mapper方法
            product = mapper.findById(id);
            //提交
            TransactionUtil.commit(session);
        } catch (Exception e) {
            //回滚
            TransactionUtil.rollback(session);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(session);
        }
        return product;
    }

    @Override
    public void update(Product product) {
        SqlSession session = null;
        try {
            //调用工具类获取session对象
            session = MapperFactory.getSqlSession();
            //获取动态代理对象
            ProductMapper mapper = MapperFactory.getMapper(session, ProductMapper.class);
            //调用mapper方法
            mapper.update(product);
            //提交
            TransactionUtil.commit(session);
        } catch (Exception e) {
            //回滚
            TransactionUtil.rollback(session);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(session);
        }
    }

    @Override
    public Boolean deleteByIds(String ids) {
        SqlSession session = null;
        //定义标记
        Boolean flag = false;
        try {
            //调用工具类获取session对象
            session = MapperFactory.getSqlSession();
            //获取动态代理对象
            ProductMapper mapper = MapperFactory.getMapper(session, ProductMapper.class);


            //处理ids数据
            String[] id  = ids.split(",");
            //调用mapper方法
            mapper.deleteByIds(id);
            //提交
            TransactionUtil.commit(session);
            flag = true;
        } catch (Exception e) {
            //回滚
            TransactionUtil.rollback(session);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(session);
        }
        return flag;
    }

    @Override
    public void openByIds(String ids) {
        SqlSession session = null;
        try {
            //调用工具类获取session对象
            session = MapperFactory.getSqlSession();
            //获取动态代理对象
            ProductMapper mapper = MapperFactory.getMapper(session, ProductMapper.class);
            //处理ids数据
            String[] id  = ids.split(",");
            //调用mapper方法
            mapper.openByIds(id);
            //提交
            TransactionUtil.commit(session);
        } catch (Exception e) {
            //回滚
            TransactionUtil.rollback(session);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(session);
        }
    }

    @Override
    public void closeByIds(String ids) {
        SqlSession session = null;
        try {
            //调用工具类获取session对象
            session = MapperFactory.getSqlSession();
            //获取动态代理对象
            ProductMapper mapper = MapperFactory.getMapper(session, ProductMapper.class);
            //处理ids数据
            String[] id  = ids.split(",");
            //调用mapper方法
            mapper.closeByIds(id);
            //提交
            TransactionUtil.commit(session);
        } catch (Exception e) {
            //回滚
            TransactionUtil.rollback(session);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(session);
        }
    }

    @Override
    public Page<Product> findAllByName(String pageNum, String pageSize, String name) {
        SqlSession session = null;
        Page<Product> page = null;
        try {
            //调用工具类获取session对象
            session = MapperFactory.getSqlSession();
            //获取动态代理对象
            ProductMapper mapper = MapperFactory.getMapper(session, ProductMapper.class);
            //默认当前页为1
            int pageN = 1;
            //默认每页显示条目数为10
            int pageS = 10;
            //对获取的当前页码和每页显示条目数进来类型处理
            if (StringUtils.isNotBlank(pageNum)) {
                pageN = Integer.parseInt(pageNum);
            }
            if (StringUtils.isNotBlank(pageSize)) {
                pageS = Integer.parseInt(pageSize);
            }
            //设置分页参数：当前页码，每页显示的条目数
            page = PageHelper.startPage(pageN, pageS);
            //调用mapper方法
            page = mapper.findAllByName(name);

        } catch (Exception e) {
            //回滚
            TransactionUtil.rollback(session);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(session);
        }
        return page;
    }
}
